﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Mindfor.Web.Data;
using Mindfor.Web.Models;

namespace Mindfor.Web
{
	/// <summary>
	/// Class used to render "Message" IView to response.
	/// </summary>
	public class MessageResult : ViewResult
	{
		/// <summary>
		/// Gets or sets text message.
		/// </summary>
		public string Message { get; set; }

		/// <summary>
		/// Gets or sets additional links to show to user.
		/// </summary>
		public List<Link> Links { get; private set; }

		/// <summary>
		/// Gets or sets auto redirect seconds. Then user will be redirected to RedirectUrl.
		/// Default is 5.
		/// </summary>
		public int AutoRedirectSeconds { get; set; }

		/// <summary>
		/// Gets or sets auto redirect url. If <c>Null</c> then auto redirect is off.
		/// </summary>
		public string AutoRedirectUrl { get; set; }

		/// <summary>
		/// Initializes new instance with null message.
		/// </summary>
		public MessageResult()
		{
			ViewName = "Message";
			Links = new List<Link>();
			AutoRedirectSeconds = 5;
		}

		/// <summary>
		/// Initializes new instance with custom title string.
		/// </summary>
		/// <param name="message">Message to render to user.</param>
		/// <param name="links">Additional links to show to user.</param>
		public MessageResult(string message, params Link[] links)
			: this()
		{
			Message = message;
			Links.AddRange(links);
		}

		/// <summary>
		/// Renders view to response.
		/// </summary>
		/// <param name="context">Controller context to render to.</param>
		public override void ExecuteResult(ControllerContext context)
		{
			ViewData.Model = new MessageModel(Message, AutoRedirectSeconds, AutoRedirectUrl, Links.ToArray());
			base.ExecuteResult(context);
		}
	}
}
